﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevEx.Thanh.Class;
using DevExpress.XtraEditors;

namespace DevEx.Thanh.UserControl
{
    public partial class frmDoiMatKhau : DevExpress.XtraEditors.XtraForm
    {
        public frmDoiMatKhau()
        {
            InitializeComponent();
        }

        private void btGhi_Click(object sender, EventArgs e)
        {
            lblPass1Error.Visible = false;
            lblPass2Error.Visible = false;
            lblPassOldError.Visible = false;
            if (CThanh_Utilities.KiemTraTextRong(txtPassold, lblPassOldError) && CThanh_Utilities.KiemTraTextRong( txtPass1 ,lblPass1Error ) && CThanh_Utilities .KiemTraTextRong( txtPass2 ,lblPass2Error))
            {
                if (txtPass1 .Text !=txtPass2 .Text )
                {
                    MessageBox.Show("Hai mật khẩu không giống nhau!");
                    lblPass1Error.Visible = true;
                    lblPass2Error.Visible = true;
                    txtPass1.Text = "";
                    txtPass2.Text = "";
                    txtPass1.Focus();
                    return;
                }
                //kiểm tra mật khẩu cũ
                CUserController ctrlU = new CUserController();
                if (ctrlU.CheckUser( GlobalVar.CurrentUser .UserName,txtPassold.Text  )==false)
                {
                    MessageBox.Show("Mật khẩu cũ không đúng");
                    lblPassOldError.Visible = true;
                    txtPassold.SelectAll();
                    txtPassold.Focus();
                }
                else
                {
                    //cập nhật password
                    tblUser _user = ctrlU.SelectRecordByID(GlobalVar.CurrentUser.ID_User);
                    _user.Password  = CThanh_Utilities.getMd5Hash(txtPass1.Text);
                    ctrlU.UpdateRecordWithoutAttach(_user );
                    this.Close();
                }
            }
        }

        private void frmDoiMatKhau_Load(object sender, EventArgs e)
        {
            lblPass1Error.Visible = false;
            lblPass2Error.Visible = false;
            lblPassOldError.Visible = false;
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}